<?php


namespace App\Http\Controllers\Admin;


use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\AdminOperateLogs;
use App\Models\Admin\AdminShopOrder;
use App\Models\Admin\AdminShopOrderList;
use App\Org\ExpressBird;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class DevOrderController extends AdminController
{
    public function devOrderList(BaseRequest $request)
    {
        $input=$request->all();
        $this->vali_rules['create_start_time']='date';
        $this->vali_rules['create_end_time']='date';
        $this->vali_rules['key']='';
        $this->vali_rules['order_status']='filled|in:0,1,2,3,4';
        $validator = Validator::make($input,$this->vali_rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $this->getPageAndSize($input);
        $list=AdminShopOrder::getList($input,$this->skip,$this->size);
        $count=AdminShopOrder::getCount($input);
        $data=[
            'list'=>$list,
            'count'=>$count,
        ];
        return $this->success($data);
    }

    public function devOrderDetail(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('admin_shop_order','id')
            ],
        ];
        $validator = Validator::make($input, $rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        try {
            $goods_info=AdminShopOrderList::getInfoByOid($id);
            $order_info=AdminShopOrder::getDetailById($id);
            $data=[
                'order_info'=>$order_info,
                'goods_info'=>$goods_info,
            ];
            return $this->success($data);
        }
        catch (\Exception $exception)
        {
            return  $this->error();
        }
    }

    public function devOrderLogisticsInfo(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('admin_shop_order','id')->whereIn('order_status',[2,3])
            ],
        ];
        $validator = Validator::make($input, $rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $info=AdminShopOrder::select('express_company_number','courier_number')->find($id);
        if(!$info->express_company_number  || !$info->courier_number)
        {
            return $this->error(['-1','订单没有快递单号']);
        }
        try {
            $params=[
                'LogisticCode'=>$info->courier_number,
                'ShipperCode'=>$info->express_company_number,
            ];
            $info=ExpressBird::queryExpressInfo($params);
            return $this->success($info);
        }
        catch (\Exception $exception)
        {
            return  $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function getExpressCompany(BaseRequest $request)
    {
        $data=[
            'list'=>[
                [
                    'express_company_number'=>'YTO',
                    'express_company_name'=>'圆通快递',
                ],
                [
                    'express_company_number'=>'STO',
                    'express_company_name'=>'申通快递',
                ],
                [
                    'express_company_number'=>'HTKY',
                    'express_company_name'=>'百世快递',
                ],
                [
                    'express_company_number'=>'HHTT',
                    'express_company_name'=>'天天快递',
                ],
            ],
        ];
        return $this->success($data);
    }

    public function devOrderDelivery(BaseRequest $request)
    {
        $input=$request->except('sign','timeStamp');
        $rules=[
            'id'=>[
                'required',
                Rule::exists('admin_shop_order','id')->where('order_status',1)
            ],
            'courier_number'=>'required',
            'express_company_number'=>'required|in:YTO,STO,HTKY,HHTT',
        ];
        $validator = Validator::make($input, $rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        DB::beginTransaction();
        try {
            $info=AdminShopOrder::find($id);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->courier_number=$input['courier_number'];
            $info->express_company_number=$input['express_company_number'];
            $info->order_status=2;
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            AdminShopOrder::addLog($perInfo,$nextInfo,6,__FUNCTION__);
            DB::commit();
            return $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    //生命周期
    public function devOrderLife(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'required',
                Rule::exists('admin_shop_order','id'),
            ],
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $list=AdminOperateLogs::getListByMainId($id,'admin_shop_order');
        $data=[
            'list'=>$list
        ];
        return  $this->success($data);
    }

}
